package com.wanxi.dangdang.controller.back.user;

import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.common.ResultCode;
import com.wanxi.dangdang.model.dto.RoleAddDto;
import com.wanxi.dangdang.model.dto.RoleUpdateByIdDto;
import com.wanxi.dangdang.model.pojo.Role;
import com.wanxi.dangdang.model.vo.RoleFindByIdVo;
import com.wanxi.dangdang.model.vo.RolePermissionVo;
import com.wanxi.dangdang.model.vo.RoleVo;
import com.wanxi.dangdang.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/8/30
 * @Version: 1.0
 */
@Api(tags={"角色接口层（需要登录）"})
@RestController
@RequestMapping("/back/a/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping("/checkName")
    @ApiOperation(value="检验权限是否存在", notes="检验是否存在,code 1001表示name已经存在")
    public CommonResult checkName(@ApiParam(value = "输入的名字", required = true) @RequestParam String name){
        String s = roleService.checkName(name);
        if(s!=null)
            return CommonResult.failure(ResultCode.PARAM_IS_INVALID);
        return CommonResult.success();
    }

    @PostMapping("/add")
    @ApiOperation(value="新增角色", notes="超级管理员添加角色,角色页面添加弹窗")
    public CommonResult add(@ApiParam(value = "新增角色参数", required = true) @RequestBody RoleAddDto roleAddDto){
        Role role = new Role();
        BeanUtils.copyProperties(roleAddDto,role);
        roleService.add(role);
        return CommonResult.success();
    }

    @GetMapping("/findById")
    @ApiOperation(value="分页查询角色列表", notes="超级管理员查询角色")
    public CommonResult<Role> findById(@ApiParam(value = "角色id", required = true) @RequestParam Long id){
        Role role = roleService.findById(id);
        RoleFindByIdVo roleFindByIdVo = new RoleFindByIdVo();
        BeanUtils.copyProperties(role,roleFindByIdVo);
        return CommonResult.success(roleFindByIdVo);
    }

    @PostMapping("/findPage")
    @ApiOperation(value="超级管理员分页查询所有角色")
    public CommonResult<List<PageInfo<RoleFindByIdVo>>> findPage(@ApiParam(value = "pageNum第几页,pageSize多少条数据", required = true) @RequestBody RoleVo roleVo){
        return CommonResult.success(roleService.findPage(roleVo));
    }

    @PostMapping("/updateById")
    @ApiOperation(value="超级管理修改角色")
    public CommonResult updateById(@ApiParam(value = "修改后的信息", required = true) @RequestBody RoleUpdateByIdDto roleUpdateByIdDto){
        Role role = new Role();
        BeanUtils.copyProperties(roleUpdateByIdDto,role);
        roleService.updateById(role);
        return CommonResult.success();
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "超级管理根据id删除角色")
    public CommonResult deleteById(@ApiParam(value = "角色id", required = true) @RequestParam Long id){
        roleService.deleteById(id);
        return CommonResult.success();
    }

    @PostMapping("/updatePermissionsById")
    @ApiOperation(value = "超级管理修改角色的权限")
    public CommonResult updatePermissionsById(@ApiParam(value = "角色id", required = true) @RequestBody RolePermissionVo rolePermissionVo){
        roleService.updatePermissionsById(rolePermissionVo);
        return CommonResult.success();
    }

    @GetMapping("/findRolesByUserId")
    @ApiOperation(value = "超级管理员通过用户id查询其所有角色")
    public CommonResult<List<Role>> findRolesByUserId(@ApiParam(value = "用户id", required = true)@RequestParam Long id){
        List<Role> list =  roleService.findRolesByUserId(id);
        return CommonResult.success(list);
    }

}
